<?php
/*
	代理商管理
	代理商的审核
	代理商禁止
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['stop']){
		$params['stop']=intval($_RQ['stop']);
		$return['params']['stop']=$_RQ['stop'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
	);
	$list=$this->pdo_getlist('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['agent_id'],'',array('nickname'=>'总店'));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'基本信息','type'=>'member'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
			array('name'=>'stop','title'=>'状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'apply_status'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'恢复','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'check','title'=>'审核'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>'20','name'=>'status','title'=>'审核状态','options'=>'apply_status'),
			array('name'=>'stop','width'=>'20','title'=>'是否禁用','type'=>'radio','options'=>'agent_status'),
			array('type'=>'choose','width'=>'20','action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['apply_status']=$this->apply_status;
	exi($return);
}
if($_RQ['op']=='stop'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->stop_agent($mid);
	exi('禁用成功！');
}
if($_RQ['op']=='start'){
	$mid=intval($_RQ['mid']);
	$this->model('member')->start_agent($mid);
	exi('恢复成功！');
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['mid']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$res=$this->model('member')->check_apply($apply_id,$status,$post['remark']);
		if($res){
			exi('操作成功！','','list');
		}else{
			exi('操作失败！','error');
		}
		
	}
	$item=$this->get('member',array('mid'=>intval($_RQ['mid'])));
	if(!$item){
		exi('指定代理商不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核代理商',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'apply_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['apply_status']=$this->apply_status;
	exi($return);
}
